/*
 * StatusView.java
 *
 * Created on 12 Август 2010 г., 21:26
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package protocol;

import DrawControls.icons.Icon;
import DrawControls.text.Parser;
import javax.microedition.lcdui.Font;

import chatty.*;
import chatty.comm.StringConvertor;
import chatty.comm.Util;
import chatty.ui.*;
import chatty.ui.menu.*;
import chatty.util.ResBundle;

import protocol.*;
import protocol.simp.*;

/**
 *
 * @author Vladimir Kryukov
 */
public class StatusView extends TextListEx implements SelectListener {
    public static final int INFO_MENU_COPY           = 1;
	public static final int INFO_MENU_COPY_ADD       = 2;
    public static final int INFO_MENU_COPY_ALL       = 3;
    public static final int INFO_MENU_BACK           = 4;
    public static final int INFO_MENU_GOTO_URL       = 5;

	public static final int INFO_MENU_GET_CLI_INFO	 = 6;
	
    public static final int ST_GET_STATUS_MSG        = 7;
    public static final int ST_GET_XSTATUS_MSG       = 8;
    private Contact contact;
	private Protocol protocol;

    /** Creates a new instance of StatusView */
    public StatusView() {
        super(null);
    }
    public void select(Select select, MenuModel model, int cmd) {
        switch (cmd) {
            case INFO_MENU_COPY:
            case INFO_MENU_COPY_ALL:
                copy(INFO_MENU_COPY_ALL == cmd);
                restore();
                break;
				
			case INFO_MENU_COPY_ADD:
                copy_add();
                restore();
                break;
				
            case INFO_MENU_BACK:
				contact.getVersion = false;
                back();
                clear();
                break;
            case INFO_MENU_GOTO_URL:
                TextListEx tx = new TextListEx("").gotoURL(getCurrText());
                if (null != tx) {
                    tx.show();
                }
                break;
				
			 case INFO_MENU_GET_CLI_INFO:
				contact.getVersion = true;
				protocol.getClientInfo(contact.getUserId());
				
				clear();
				int id = uiBigTextIndex++;
				Parser line = createParser(id);
				line.addText(ResBundle.getString("wait"), THEME_PARAM_VALUE, FONT_STYLE_PLAIN);
				addPar(line);
				restore();
                break;	
				
            /*// #sijapp cond.if protocols_ICQ is "true" #
            case ST_GET_STATUS_MSG:
                ((IcqContact)contact).requestStatusMessage();
                restore();
                break;
            // #sijapp cond.if modules_XSTATUSES is "true" #
            case ST_GET_XSTATUS_MSG:
                ((IcqContact)contact).requestXStatusMessage();
                restore();
                break;
            // #sijapp cond.end #
            // #sijapp cond.end #
			*/
        }
    }

    public void addClient(Icon icon, String name) {
        addPlain(icon, name);
    }
    /////////////////////
	public void addBr() {
        Parser line = createParser(-1);
        line.addText(" \n", THEME_TEXT, FONT_STYLE_PLAIN);
        addPar(line);
    }
    public void addPlain(Icon img, String str) {
        if (!StringConvertor.isEmpty(str)) {
            int id = uiBigTextIndex++;
            Parser line = createParser(id);
            if (null != img) {
                line.addImage(img);
            }
            line.addText(str, THEME_TEXT, FONT_STYLE_PLAIN);
            addPar(line);
        }
    }
    public void addStatusText(String text) {
        if (null != text) {
            int id = uiBigTextIndex++;
            Parser line = createParser(id);
            line.addText(text, THEME_PARAM_VALUE, FONT_STYLE_PLAIN);
            addPar(line);
        }
    }
    public void addInfo(String key, String value) {
        addParam(key, StringConvertor.notNull(value));
    }
    /////////////////////
    public void addStatus(Status status) {
        StatusInfo info = protocol.getStatusInfo();
        addStatus(info.getIcon(status), info.getName(status));
    }
    public void addStatus(Icon icon, String name) {
        addPlain(icon, name);
    }
    protected MenuModel getMenu() {
        MenuModel menu = new MenuModel(MenuModel.MENU_LEFT);
        menu.addItem("copy_text",     Chatty.chatIcons.iconAt(Chatty.ICON_COPY), INFO_MENU_COPY);
		if (!ChattyUI.isClipBoardEmpty()) {
		    menu.addItem("copy_add",      Chatty.chatIcons.iconAt(Chatty.ICON_COPY_ADD), INFO_MENU_COPY_ADD);
		}
        menu.addItem("copy_all_text", Chatty.chatIcons.iconAt(Chatty.ICON_COPY_ALL), INFO_MENU_COPY_ALL);
        if ((1 < getCurrTextIndex()) && Util.hasURL(getCurrText())) {
            menu.addItem("goto_url", Chatty.chatIcons.iconAt(Chatty.ICON_GOTO_URL), INFO_MENU_GOTO_URL);
        }
		menu.addItem("get_cli_info", Chatty.icons.iconAt(Chatty.ICON_USER_INFO), INFO_MENU_GET_CLI_INFO);
		
        ///contact
        /*// #sijapp cond.if protocols_ICQ is "true" #
        if ((contact instanceof IcqContact) && contact.isOnline()) {
            IcqContact c = (IcqContact)contact;
            menu.addItem("reqstatmsg", ST_GET_STATUS_MSG);
            // #sijapp cond.if modules_XSTATUSES is "true" #
            if (XStatus.noneXStatus != c.getXStatus()) {
                menu.addItem("reqxstatmsg", ST_GET_XSTATUS_MSG);
            }
            // #sijapp cond.end #
        }
        // #sijapp cond.end #*/
        menu.addItem("back", Chatty.icons.iconAt(Chatty.ICON_BACK), INFO_MENU_BACK);
        menu.setActionListener(this);
        return menu;
    }
    public void init(Contact c, Protocol p) {
        contact = c;
		protocol = p;
        setMenuCodes(INFO_MENU_BACK, INFO_MENU_COPY);
        lock();
        clear();
        setCaption(contact.getName());
        addInfo(contact.getProtocol().getUinName(), contact.getUserId());
		
		

		Parser line = createParser(15);
		line.doCRLF();
		line.addText(ResBundle.getString("client_info"), THEME_TEXT, FONT_STYLE_BOLD);
        addPar(line);
		
		addInfo("client_name", c.clientName);
		addInfo("client_ver", c.clientVer);
		addInfo("client_build", c.clientBuild);
		addInfo("client_os", c.clientOs);
		addInfo("phone_model", c.phoneModel);
		addBr();
    }
    public Contact getContact() {
        return contact;
    }
    public void showIt() {
        unlock();
        super.show();
    }
}
